/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//



#ifndef _HDFS_LIBHDFS3_CLIENT_OUTPUTSTREAMINTER_H_
#define _HDFS_LIBHDFS3_CLIENT_OUTPUTSTREAMINTER_H_

#include <kmhdfs/common/exception_internal.h>
#include <kmhdfs/client/file_system_inter.h>
#include <memory>
#include <kmhdfs/client/permission.h>

namespace Hdfs {
namespace Internal {

/**
 * A output stream used to write data to hdfs.
 */
class OutputStreamInter {
public:
    virtual ~OutputStreamInter() {
    }

    /**
     * To create or append a file.
     * @param fs hdfs file system.
     * @param path the file path.
     * @param flag creation flag, can be Create, Append or Create|Overwrite.
     * @param permission create a new file with given permission.
     * @param createParent if the parent does not exist, create it.
     * @param replication create a file with given number of replication.
     * @param blockSize  create a file with given block size.
     */
    virtual void open(std::shared_ptr<FileSystemInter> fs, const char * path,
                      std::pair<std::shared_ptr<LocatedBlock>, std::shared_ptr<Hdfs::FileStatus>> & pair,
                      int flag, const Permission & permission, bool createParent, int replication,
                      int64_t blockSize, int64_t fileId) = 0;

    /**
     * To append data to file.
     * @param buf the data used to append.
     * @param size the data size.
     */
    virtual void append(const char * buf, int64_t size) = 0;

    /**
     * Flush all data in buffer and waiting for ack.
     * Will block until get all acks.
     */
    virtual void flush() = 0;

    /**
     * return the current file length.
     * @return current file length.
     */
    virtual int64_t tell() = 0;

    /**
     * @ref OutputStream::sync
     */
    virtual void sync() = 0;

    /**
     * close the stream.
     */
    virtual void close() = 0;

    virtual std::string toString() = 0;

    virtual void setError(const exception_ptr & error) = 0;
};

}
}

#endif /* _HDFS_LIBHDFS3_CLIENT_OUTPUTSTREAMINTER_H_ */
